package com.scf.fruit.business.login.bo;

import com.alibaba.fastjson.JSONObject;
import com.scf.fruit.business.login.po.WxResultRes;
import com.scf.fruit.enums.ExceptionCodeEnum;
import com.scf.fruit.user.WxUserInfoDto;
import com.scf.fruit.user.WxUserInfoService;
import com.scf.fruit.util.*;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author : 孙超飞
 * @title :
 * @description :
 * @copyright :
 * @date : 2018-05-18 14:11
 */
@Component
public class LoginBo {
    @Value("${wx.appId}")
    private String appId;
    @Value("${wx.appSecret}")
    private String appSecret;
    @Value("${wx.loginUrl}")
    private String loginUrl;

    @Resource
    private WxUserInfoService wxUserInfoService;

    public ResponseVo login( String code){

        StringBuffer sb=new StringBuffer(loginUrl);
        sb.append("appid=").append(appId).append("&secret=").append(appSecret).append("&js_code=").append(code).append("&grant_type=authorization_code");
        String s = HttpClientUtil.doGet(sb.toString());
        WxResultRes wxResultRes = JSONObject.parseObject(s, WxResultRes.class);
        if(!StringUtils.isEmpty(wxResultRes.getErrcode())&&wxResultRes.getErrcode().equals(40029)) {
            return new ResponseVo(ExceptionCodeEnum.EX_WX_CODE_ERROR.getValue(),ExceptionCodeEnum.EX_WX_CODE_ERROR.getMsg());
        }
        if(!StringUtils.isEmpty(wxResultRes.getOpenid())&&!StringUtils.isEmpty(wxResultRes.getSession_key()))
        {
            CacheUtil.put(code,wxResultRes);
            ResponseVo res = new ResponseVo();
            return res;
        }else
        {
            return new ResponseVo(String.valueOf(wxResultRes.getErrcode()),wxResultRes.getErrmsg());
        }

    }

    public ResponseVo saveUserInfo(String code,String encryptedData,String iv) throws Exception {
        ResponseVo res=new ResponseVo();
        WxResultRes o = (WxResultRes) CacheUtil.get(code);
        if(StringUtils.isEmpty(encryptedData)||StringUtils.isEmpty(iv)||StringUtils.isEmpty(code)||StringUtils.isEmpty(o.getSession_key())){
            return new ResponseVo(ExceptionCodeEnum.EX_PARAM_ERROR.getValue(),ExceptionCodeEnum.EX_PARAM_ERROR.getMsg());
        }
        byte[] decode = Base64.decodeBase64(encryptedData);
        byte[] decode1 = Base64.decodeBase64(iv);
        byte[] decode2 = Base64.decodeBase64(o.getSession_key());
        String str = AESCodec.decrypt(decode, decode2, decode1);
        WxUserInfoDto wxUserInfoDto = JSONObject.parseObject(str, WxUserInfoDto.class);

         List<WxUserInfoDto> wxUserInfoDtos = wxUserInfoService.queryUserByOpenId(wxUserInfoDto.getOpenId());
        if(!CollectionUtils.isEmpty(wxUserInfoDtos)){
            res.setCode(ExceptionCodeEnum.EX_USER_EXIST_ERROR.getValue());
            res.setCode(ExceptionCodeEnum.EX_USER_EXIST_ERROR.getMsg());
            res.setData(wxUserInfoDtos.get(0).getId().toString());
            return res;
        }
        wxUserInfoDto.setId(IDUtil.getId());
        if(wxUserInfoService.insertUser(wxUserInfoDto))
        {
            res.setData(wxUserInfoDto.getId().toString());
        }else {
            res.setCode(ExceptionCodeEnum.EX_USER_ERROR.getValue());
            res.setCode(ExceptionCodeEnum.EX_USER_ERROR.getMsg());
        }
        return res;
    }

}
